package com.android.test.download;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.test.R;
import com.android.test.download.Downloader.OnDownloadListener;

public class DownloadActivity extends Activity implements OnClickListener, OnDownloadListener {

	private Button start;
	private Button stop;

	private TextView contentLengthText;
	private TextView completedSizeText;
	private ProgressBar progressBar;

	Downloader downloader;

	private String httpUrl = "http://192.168.0.156/ShiYouBao.apk";
	private String fileName = "zzz.apk";
	private String path;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_download);
		start = (Button) findViewById(R.id.start);
		stop = (Button) findViewById(R.id.stop);

		contentLengthText = (TextView) findViewById(R.id.contentLengthText);
		completedSizeText = (TextView) findViewById(R.id.completedSizeText);

		progressBar = (ProgressBar) findViewById(R.id.progressBar);

		start.setOnClickListener(this);
		stop.setOnClickListener(this);

		downloader = new Downloader();
		downloader.setOnDownloadListener(DownloadActivity.this);

	}

	@Override
	public void onClick(View v) {
		try {
			if (v == start) {
				File rootFile = Environment.getExternalStorageDirectory();
				File file = new File(rootFile, fileName);
				if (!file.exists()) {
					file.createNewFile();
				}
				path = file.getPath();
				new Thread(new Runnable() {

					@Override
					public void run() {
						downloader.download(httpUrl, path);
					}
				}).start();
			} else if (v == stop) {
				if (!downloader.isCancel()) {
					downloader.cancel(true);
				} else {
					new Thread(new Runnable() {

						@Override
						public void run() {
							downloader.download(httpUrl, path);
						}
					}).start();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void p(String s) {
		System.out.println(s);
	}

	private void printFileInfo() {

		// File[] files = rootFile.listFiles();
		// for (File file : files) {
		// p("===================================");
		// p("getAbsoluteFile:" + file.getAbsoluteFile());
		// p("getAbsolutePath:" + file.getAbsolutePath());
		// p("getCanonicalFile:" + file.getCanonicalFile());
		// p("getCanonicalPath:" + file.getCanonicalPath());
		// p("getName:" + file.getName());
		// p("getParent:" + file.getParent());
		// p("getParentFile:" + file.getParentFile());
		// p("getPath:" + file.getPath());
		//
		// }
	}

	@Override
	public void onContentLength(int length) {
		final int size = length;
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				contentLengthText.setText(byteToKb(size));
				progressBar.setMax(size);
			}
		});
	}

	@Override
	public void onProgressChanage(int completedSize) {
		final int size = completedSize;
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				completedSizeText.setText(byteToKb(size));
				progressBar.setProgress(size);
			}
		});
	}

	private String byteToKb(int size) {
		return (size / 1024 / 1024) + "MB";
	}

	private int bytes(int size) {
		int surplus = 0;
		if ((surplus = size % 1024) >= 1024) {
			bytes(surplus);
		} else {
			return size % 1024;
		}
		return 0;
	}
}
